import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;

public class MyBinaTree {
    public static class TreeNode{
        char val;
        TreeNode left;
        TreeNode right;

        public TreeNode(char val) {
            this.val = val;
        }
    }
    public TreeNode root;
    public TreeNode createTree(){
        TreeNode A = new TreeNode('A');
        TreeNode B = new TreeNode('B');
        TreeNode C = new TreeNode('C');
        TreeNode D = new TreeNode('D');
        TreeNode E = new TreeNode('E');
        TreeNode F = new TreeNode('F');
        TreeNode G = new TreeNode('G');
        TreeNode H = new TreeNode('H');
        A.left = B;
        A.right = C;
        B.left = D;
        B.right = E;
        C.left = F;
        C.right = G;
        E.right = H;
        return A;
    }
    public void preOrder(TreeNode root){
        if(root == null) return;
        System.out.print(root.val + " ");
        preOrder(root.left);
        preOrder(root.right);
    }
    public void inOrder(TreeNode root){
        if(root == null) return;
        inOrder(root.left);
        System.out.print(root.val + " ");
        inOrder(root.right);
    }
    public void postOrder(TreeNode root){
        if(root == null) return;
        preOrder(root.left);
        preOrder(root.right);
        System.out.print(root.val + " ");
    }

//    public List<TreeNode> preOrder2(TreeNode root){
//        if(root == null) return list;
//        list.add(root);
//        preOrder2(root.left);
//        preOrder2(root.right);
//        return list;
//    }
    public List<TreeNode> preOrder2(TreeNode root){
        List<TreeNode> list = new ArrayList<>();
        if(root == null) return list;
        list.add(root);
        List<TreeNode> leftList = preOrder2(root.left);
        list.addAll(leftList);
        List<TreeNode> rightList = preOrder2(root.right);
        list.addAll(rightList);
        return list;
    }
    public int getSize(TreeNode root){
        return getSize(root.left)+
                getSize(root.right) + 1;
    }
    public int getLeafSize2(TreeNode root){
        if(root == null) return 0;
        if(root.left == null && root.right == null){
            return 1;
        }
        return getLeafSize2(root.left)+
                getLeafSize2(root.right);
    }
    public int getKLeverCount(TreeNode root,int k){
         if(root == null) return 0;
         if(k == 1){
             return 1;
         }
         return getKLeverCount(root.left ,k - 1)+
                 getKLeverCount(root.right,k - 1);
    }
    public int getHigh(TreeNode root){
        if(root == null) return 0;
        if(root.left == null && root.right == null) return 1;
        return getHigh(root.left) > getHigh(root.right)?getHigh(root.left):getHigh(root.right) + 1;
    }
    public boolean find(TreeNode root,char key){
        if(root == null) return false;
        if(root.val == key){
            return true;
        }

    }
    public boolean yiYang(TreeNode q,TreeNode p){
        if(q == null && p != null || q != null && p == null){
            return false;
        }
        if(q == null && p == null){
            return true;
        }
        if(q.val == p.val){
            return yiYang(q.left,p.left) &&
                    yiYang(q.right,p.right);
        }else{
            return false;
        }
    }
    //二叉树层序遍历
    public List<List<TreeNode>> leverOder(TreeNode root){
        List<List<TreeNode>> ret = new ArrayList<>();
        if(root == null) return null;
        Queue<TreeNode> qu = new LinkedList<>();
        qu.offer(root);
        while(!qu.isEmpty()){
            List<TreeNode> tmp = new ArrayList<>();
            int i = qu.size();
            while(i != 0){
                TreeNode cur = qu.poll();
                tmp.add(cur);
                i--;
                if(cur.left != null){
                    qu.offer(cur.left);
                }
                if(cur.right != null){
                    qu.offer(cur.right);
                }
            }
            ret.add(tmp);
        }
        return ret;
    }
    //根据前序遍历和中续遍历，构建二叉树
    private static int index;
    public TreeNode buildTree(char[] preOrder ,char[] inOrder){
        index = 0;
        int len = preOrder.length;
        return buildTreeChild(preOrder,inOrder,0,len - 1);
    }
    public TreeNode buildTreeChild(char[] preOrder,char[] inOrder,int inbegin,int inend){
        //1判断有没有子树
        if(inbegin > inend){
            return null;
        }
        //2创建根节点
        TreeNode root = new TreeNode(preOrder[index]);
        //3从中序遍历中找到根节点所在下标
        int i = fineIndex(inOrder,inbegin,inend,preOrder[index]);
        //4创建左子树和右子树
        root.left = buildTreeChild(preOrder,inOrder,0,i - 1);
        root.right = buildTreeChild(preOrder,inOrder,i + 1,inend);
        return root;
    }
    //从中序遍历中找到根节点所在下标
    public int fineIndex(char[] inOrder,int inbegin,int inend,char key){
        for (int i = inbegin; i <= inend; i++) {
            if(inOrder[i] == key){
                return i;
            }
        }
        return -1;
    }
    //根据中序遍历和后续遍历，构建二叉树
    public TreeNode postbuildTree2(char[] postOrder ,char[] inOrder){
        int len = inOrder.length;
        index = len - 1;
        return buildTreeChild2(postOrder,inOrder,0,len - 1);
    }
    public TreeNode buildTreeChild2(char[] postOrder,char[] inOrder,int inbegin,int inend){
        //1判断有没有子树
        if(inbegin > inend){
            return null;
        }
        //2创建根节点
        TreeNode root = new TreeNode(postOrder[index]);

        //3从中序遍历中找到根节点所在下标
        int i = fineIndex(inOrder,inbegin,inend,root.val);
        index--;
        //4创建左子树和右子树
        root.right = buildTreeChild2(postOrder,inOrder,i + 1,inend);
        root.left = buildTreeChild2(postOrder,inOrder,inbegin,i - 1);
        return root;
    }
}
